// http://jscompress.com/
// Author: Sergi Polonio
//Requiere jquery.json


//////////////////////////////////////////
/////////  jPost for ServicesPHP //////
//////////////////////////////////////////

// Usage:
//======

// jPost(datasend, service, callback)
        //datasend : Data send to services
        //service: Service name in php page
        //callback: Optional function that receives a response parameter (string) in JSON format.
//
// dataSend is an object with member shipping "method" required.
// Method is the name of the service function call.

//EXAMPLE: request = (name : 'Dorothy', method: 'newPeople')
//jPost(request, 'people', function(response){ alert(response.result) })

/*set={path:'',ext:''}**/

/* 
 *  object ini. <b>set</b> = {<b>path</b>:'',<b>ext</b>:''}<br />
 *
 *  <b>path</b> is path of services     <br />
 *  <b>ext</b> is  extension of services<br />
 */
function jPost(set){

    this.settings = {
        
        path :set.path? set.path : '',
        ext  :set.ext?  set.ext : ''
    }
    /*
     *<b>send</b>(<b>datasend</b>, <b>service</b>, <b>callback</b>)<br/>
     * . <b>datasend </b>: Data send to services<br/>
     * . <b>service</b>: Service name in page response<br/>
     * . <b>callback</b>: Optional function that receives a response parameter (string) in JSON format.
     */
    this.send = function(datasend, service , callback){

        $.ajax({
            async: true,
            type: 'POST',
           
           contentType: "application/x-www-form-urlencoded; charset=utf-8",
            url: this.settings.path + service + this.settings.ext,
            datatype: "json",
            data:  "data="+$.toJSON(datasend),
            success: function(dataResult){

               if( typeof(dataResult) != "null"){

                     response = $.parseJSON(dataResult);
                     if(callback){
                        callback(response);
                     }
                }
            }
        });
    }
}
/*
 *ini: {path:'./@_services/', ext: '.service.php'}
 */
jpost = new jPost({path:'./@_services/', ext: '.service.php'});

/**
 *fn jQuery, recorre selector y devuelve objeto [@name][#id]
 */
$.fn.toObject = function (){
    
     var object = {};

     object[$(this).eq(0).attr('name')] = {};

    $(this).each(function(){       
       object[$(this).attr('name')] [$(this).attr('id')] = $(this).val();      
    });

    return object[$(this).attr('name')];

}


